package org.leetcode.middle.leetcode128;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class Solution
{

    public int longestConsecutive3(int[] nums){
        Set<Integer> set = new HashSet<>();

        for (int num:nums) {
            set.add(num);
        }

        int res = 0;

        for (int num:nums) {

            //只考虑数组中连续序列的最小值
            if (!set.contains(num - 1)){
                int longest = 1;
                int currentNum = num+1;

                while (set.contains(currentNum)){
                    longest++;
                    currentNum++;
                }

                res=Math.max(res,longest);
            }

        }
        return res;
    }

    public int longestConsecutive2(int[] nums) {

        Set<Integer> set = new HashSet<>();

        for (int num:nums) {
            set.add(num);
        }

        int resLength = 0;

        for (int num:set) {
            //只考虑比当前数大的数字这种情况
            if (!set.contains(num-1)) {
                int maxLength=1;
                int currentNum = num;

                while (set.contains(currentNum+1)) {
                    maxLength++;
                    currentNum++;
                }
                resLength =Math.max(maxLength,resLength);
            }

        }

        return resLength;

    }



    /**
     * 失败
     */
    public int longestConsecutive(int[] nums)
    {

        int length = nums.length;

        Set<Integer> set = new HashSet<>();

        for (int num:nums) {
            set.add(num);
        }

        int index =1;

        for (int i = 0; i < length; i++) {
            int numPre =nums[i]-1;
            int numNext = nums[i]+1;

            if (set.contains(numPre)) {
                index++;
                numPre--;
            }
            if (set.contains(numNext)) {
                index++;
            }

        }
        return 2;
    }
}
